package com.bianyuanren.common.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class Jpush {

    // 设置好账号的app_key和masterSecret是必须的
    private static String APP_KEY = "e142ee60b72208561eda3815";
    private static String MASTER_SECRET = "14bd8e8769660a735ac5b865";

    //极光推送>>all  极光推送的全部用户推送
    public static void jpushAll(Map<String, String> parm) {
        //创建JPushClient(极光推送的实例)
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.all())//你项目中的所有用户
                //.setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //这里是指定开发环境,不用设置也没关系
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //发送内容
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();
        try {
            PushResult pu = jpushClient.sendPush(payload);
            System.out.println(pu);
        } catch (APIConnectionException e) {
            log.error("消息推送异常：{}", e.getStackTrace());
        } catch (APIRequestException e) {
            log.error("消息推送异常：{}", e.getStackTrace());
        }
    }

    public static void jpushByRegId(Map<String, String> parm) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())//指定android平台的用户
//                .setAudience(Audience.all())//你项目中的所有用户
                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //这里是指定开发环境,不用设置也没关系
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //发送内容
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();
        try {
            PushResult pu = jpushClient.sendPush(payload);
            System.out.println(pu);
        } catch (APIConnectionException e) {
            log.error("消息推送异常：{}", e.getStackTrace());
        } catch (APIRequestException e) {
            log.error("消息推送异常：{}", e.getStackTrace());
        }
    }

    //所有平台的所有用户
    public static void jpushAndroidAndIos(Map<String, String> parm) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())//所有平台
                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder() //发送ios
                                .setAlert(parm.get("msg")) //消息体
                                // .setBadge(+1)
                                .incrBadge(0)
                                .setSound("happy") //ios提示音
                                .addExtras(parm) //附加参数
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder() //发送android
                                .addExtras(parm) //附加参数
                                .setAlert(parm.get("msg")) //消息体
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(false).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
                .build();
        try {
            PushResult pu = jpushClient.sendPush(payload);
            System.out.println(pu);
        } catch (APIConnectionException e) {
            log.error("消息推送异常：{}", e.getStackTrace());
        } catch (APIRequestException e) {
            log.error("消息推送异常：{}", e.getStackTrace());
        }
    }



    public static void main(String[] args) {
        //设置推送参数
        //这里同学们就可以自定义推送参数了
        Map<String, String> parm = new HashMap<String, String>();
        //这里的id是,移动端集成极光并登陆后,极光用户的rid
        parm.put("id", "testAlias");
        parm.put("title", "43434");
        parm.put("url", "/pages/msg/msg");
        //设置提示信息,内容是文章标题
        parm.put("msg", "123456");
       // Jpush.jpushAll(parm);
       //  Jpush.jpushByRegId(parm);
        Jpush.jpushAndroidAndIos(parm);
    }
}
